
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.bo.IEmpsalaryperiodBo;
import com.wyzz.hr.dao.IEmpsalaryperiodDAO;
import com.wyzz.hr.pojo.Empsalaryperiod;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;

import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;


public class EmpsalaryperiodBoImpl implements IEmpsalaryperiodBo {

	public EmpsalaryperiodBoImpl() {
	}

	public List getAllYears() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryperiod.class);
		List yearmonth = empsalaryperiodDAO.findByCriteria(detachedCriteria
				.setProjection(Projections.distinct(Projections
						.min("espdYearmonth"))));
		if (yearmonth == null || yearmonth.size() == 0)
			return null;
		Integer endYear = Integer.valueOf(DateUtil.getYear(new Date()));
		Integer startYear = endYear;
		if (DateUtil.getMonth(new Date()) >= 11)
			endYear = Integer.valueOf(endYear.intValue() + 1);
		if (yearmonth != null && yearmonth.size() > 0
				&& yearmonth.get(0) != null)
			startYear = Integer.decode(((String) yearmonth.get(0)).substring(0,
					4));
		List year = new ArrayList();
		for (Integer i = startYear; i.intValue() <= endYear.intValue();) {
			year.add(i.toString());
			Integer integer = i;
			Integer integer1 = i = Integer.valueOf(i.intValue() + 1);
			Integer _tmp = integer;
		}

		return year;
	}

	public List getPaidPeriod(Integer status, String startYearMonth,
			String endYearMonth) {
		if (StringUtils.isEmpty(startYearMonth)
				|| StringUtils.isEmpty(endYearMonth))
			return null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryperiod.class);
		BaseCrit.addDC(dc, Empsalaryperiod.PROP_ESPD_YEARMONTH, "between",
				new String[] { startYearMonth, endYearMonth });
		if (status != null)
			BaseCrit.addDC(dc, Empsalaryperiod.PROP_ESPD_STATUS, "eq",
					new Integer[] { status });
		return empsalaryperiodDAO.findByCriteria(dc.setProjection(Projections
				.distinct(Projections
						.property(Empsalaryperiod.PROP_ESPD_YEARMONTH))));
	}

	public List getPayPeriods(Integer status, String yearMonthArray[]) {
		if (yearMonthArray == null || yearMonthArray.length == 0)
			return null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryperiod.class);
		BaseCrit.addDC(dc, Empsalaryperiod.PROP_ESPD_YEARMONTH, "in",
				yearMonthArray);
		if (status != null)
			BaseCrit.addDC(dc, Empsalaryperiod.PROP_ESPD_STATUS, "eq",
					new Integer[] { status });
		return empsalaryperiodDAO.findByCriteria(dc);
	}

	public IEmpsalaryperiodDAO getEmpsalaryperiodDAO() {
		return empsalaryperiodDAO;
	}

	public void setEmpsalaryperiodDAO(IEmpsalaryperiodDAO empsalaryperiodDAO) {
		this.empsalaryperiodDAO = empsalaryperiodDAO;
	}

	public Integer getEspdStatus(String yearmonth) {
		Empsalaryperiod espd = loadEspdStatus(yearmonth);
		if (espd != null)
			return espd.getEspdStatus();
		else
			return new Integer(3);
	}

	public Integer getEspdStatusNew(String yearmonth) {
		Empsalaryperiod espd = loadEspdStatus(yearmonth);
		if (espd != null)
			return espd.getEspdStatus();
		else
			return null;
	}

	public Empsalaryperiod loadEspdStatus(String yearmonth) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryperiod.class);
		detachedCriteria.add(Restrictions.eq(
				Empsalaryperiod.PROP_ESPD_YEARMONTH, yearmonth));
		detachedCriteria.addOrder(Order.desc(Empsalaryperiod.PROP_ESPD_STATUS));
		List result = empsalaryperiodDAO.findByCriteria(detachedCriteria);
		if (result.size() > 0)
			return (Empsalaryperiod) result.get(0);
		else
			return null;
	}

	public void updateConfirmSubmit(String yearmonth, Integer status) {
		Empsalaryperiod period = loadEspdStatus(yearmonth);
		if (status.intValue() == 2 && period.getEspdStatus().intValue() == 1) {
			period.setEspdStatus(status);
			empsalaryperiodDAO.updateObject(period);
		} else if (status.intValue() == 0
				&& period.getEspdStatus().intValue() == 1) {
			period.setEspdStatus(status);
			empsalaryperiodDAO.updateObject(period);
		} else if (status.intValue() == 1
				&& period.getEspdStatus().intValue() == 0) {
			period.setEspdStatus(status);
			empsalaryperiodDAO.updateObject(period);
		} else if (status.intValue() == 0
				&& period.getEspdStatus().intValue() == 2) {
			period.setEspdStatus(status);
			empsalaryperiodDAO.updateObject(period);
		}
	}

	public boolean saveOrupdateObject(Object obj) {
		try {
			empsalaryperiodDAO.saveOrupdate(obj);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private IEmpsalaryperiodDAO empsalaryperiodDAO;
}
